package com.dlc.felear.xiaoensale.activity;

import android.text.InputType;
import android.view.View;
import android.widget.ListView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.MenuTypeAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
import com.dlc.felear.xiaoensale.dao.MenuTypeDao;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.MenuType;
import com.dlc.felear.xiaoensale.helper.Session;
import com.dlc.felear.xiaoensale.utils.UuidUtil;
import com.itdlc.android.library.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 食材选择页面
 */
public class MenuFlavourTypeListActivity extends BaseActivity implements SingleInputDialog.SingleDialogCallback {
    public static final int CODE = 1024;
    @BindView(R.id.e_gv)
    ListView gv;
    private MenuTypeAdapter adapter;
    private TopbarHelper topbarHelper;
    private MenuTypeDao menuTypeDao;
    private int src = 0;
    private SingleInputDialog singleInputDialog;
    private Session session;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_food_material_list_type;
    }

    @Override
    protected void initialView() {
        session = Session.getSession();
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setTitle("菜谱类别");
        topbarHelper.setReturnEnable(true);
        topbarHelper.setNavActivity(LoadDataActivity.class);
        topbarHelper.setcon(R.drawable.arrow_left);
        adapter = new MenuTypeAdapter(this);
        gv.setAdapter(adapter);
        singleInputDialog = new SingleInputDialog(this, this);
        singleInputDialog.setEtInputType(InputType.TYPE_CLASS_TEXT);
        menuTypeDao = new MenuTypeDao(this);
    }


    @OnClick(R.id.topbar_add)
    public void clickView(View v) {
        singleInputDialog.setTips("分类");
        singleInputDialog.setHintMsg("请输入分类名称");
        singleInputDialog.showDialog(R.id.topbar_add);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void initData() {
        src = getIntent().getIntExtra("src", 0);
        adapter.setValue(src, CODE);
        adapter.setItems(menuTypeDao.queryForAll());
    }


    @Override
    public void SingleDialogInputResult(String value, int id) {
        if (id == R.id.topbar_add) {
            MenuType menuType = menuTypeDao.queryForId(UuidUtil.getUuidByName(value));
            if (menuType != null) {
                showShortToast("分类名字已存在,请重新输入");
                return;
            }
            String idd = UuidUtil.getUuidByName(session.getString(Constant.CANTEENTID) + value);
            menuTypeDao.add(new MenuType(idd, value, 1));
            adapter.setItems(menuTypeDao.queryForAll());
        }
    }


}